package persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import modelo.Login;

public class DAOLogin {
    
    private Connection conexao;
    
    private void obterConexao(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexao = DriverManager.getConnection("jdbc:mysql://localhost/sirp", "root", "123");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    private void liberarConexao(){
        try {
            conexao.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
        
    public void inserir(Login login){
        obterConexao();
        try {
            PreparedStatement pstat = conexao.prepareStatement("INSERT Login (usuario, senha, nivel) VALUES (?, ?, ?)");
            pstat.setString(1, login.getLogin());
            pstat.setString(2, login.getSenha());
            pstat.setString(3, login.getNivel());
            
            pstat.executeUpdate();
                  
        } catch (SQLException e) {
            e.printStackTrace();
        }
        liberarConexao();
    }
    
    public void atualizar(Login login){
        obterConexao();
        try {
        	 PreparedStatement stat = conexao.prepareStatement("UPDATE Profissional SET nome = ?, sobrenome = ?, sexo = ?, idade = ?, dataNascimento = ?, estadoCivil = ?, profissao = ?, estudante = ?, fumante = ?, filhos = ?, moro = ?, hobby = ?, descricao = ?, telefoneCelular = ?, telefoneComercial = ?, telefoneResidencial = ?, webSite = ? WHERE email = ? ");
             
        	 stat.setString(1, login.getLogin());
             stat.setString(2, login.getSenha());
             stat.setString(3, login.getNivel());
             
             stat.executeUpdate();
                                       
        } catch (SQLException e) {
            e.printStackTrace();
        }
        liberarConexao();
    }
    
    public Login buscarLogin(String usuario, String senha){
        obterConexao();
        try {
            PreparedStatement stat = conexao.prepareStatement("SELECT * FROM Login WHERE usuario = ? and senha = ?");
            stat.setString(1, usuario);
            stat.setString(2, senha);
            ResultSet set = stat.executeQuery();
                                                   
            if (set.next()){
            	Login login = new Login();
            	login.setLogin(set.getString("login"));
            	login.setSenha(set.getString("senha"));
            	login.setNivel(set.getString("nivel"));
               
            return login;	
            }  
            
            } catch (SQLException e) {
                e.printStackTrace();
            }
            liberarConexao();
        return null;
        }
    
}